import _ from 'lodash'
import ebus from 'libs/ebus.js'
import {streamSrcApi} from '../../api.js'
import * as types from '../mutation-types'

//ipcam
export function doLoadIpcams({dispatch}) {
  let extendQuery = {
    filterDisable: 1
  }
  dispatch(types.REFRESH_IPCAMS_START)
  streamSrcApi.getItems('ipcameras', {extendQuery: JSON.stringify(extendQuery)}).then(function(res) {
    if (res && res.data && res.data.code === 0) {
      dispatch(types.SET_IPCAMS, res.data.data.ipcameras)
    }
    dispatch(types.REFRESH_IPCAMS_END)
  })
}

//ipcam verdor
function makeVendor(vendor) {
  vendor.productModelsStr = ''
  vendor.productModelsEdit = ''
  let models = JSON.parse(vendor.productModels)
  if (Array.isArray(models)) {
    for (var i = 0; i < models.length; i++) {
      vendor.productModelsStr += "<p class='my-table-textarea'>" + models[i] + "</p>"
      if (i > 0) {
        vendor.productModelsEdit += '|'
      }
      vendor.productModelsEdit += models[i]
    }
  }
}

export function doLoadVendors({dispatch}) {
  dispatch(types.REFRESH_IPCAM_VENDORS_START)
  streamSrcApi.getItems('ipcamVendorCfgs').then(function(res) {
    if (res && res.data && res.data.code === 0) {
      let vendors = _.forEach(res.data.data.ipcamVendorCfgs, (item) => {
        makeVendor(item)
      })
      dispatch(types.SET_IPCAM_VENDORS, vendors)
    }
    dispatch(types.REFRESH_IPCAM_VENDORS_END)
  })
}

var index = 0
function addStreams(dispatch, srcType, items, useItemSrc) {
  let streams = _.map(items, (item) => {
    index++
    return {
      _id: index, 
      _checked: false,
      topoId: useItemSrc ? item.topoId : 0,
      name: item.name,
      portName: useItemSrc ? item.portName : `${srcType}_${item._id}`,
      mainUrl: item.mainUrl,
      previewUrl: item.previewUrl,
      srcType: srcType        
    }    
  })
  dispatch(types.ADD_STREAM_SRCS, srcType, streams)
}

export function setStreamChecked({dispatch}, id, val) {
  dispatch(types.SET_STREAM_SRC_CHECKED, id, val)
}

export function setStreamAllUnchecked({dispatch}) {
  dispatch(types.SET_STREAM_SRC_ALL_UNCHECKED)
}

//softenc
export function doLoadSoftEncs({dispatch, state}, {init = false} = {}) {
  if (init && state.softenc.init) {
    return
  }
  dispatch(types.REFRESH_SOFTENCS_START)
  streamSrcApi.getItems('softencs').then(function(res) {
    if (res && res.data && res.data.code === 0) {
      dispatch(types.SET_SOFTENCS, res.data.data.softencs)
      addStreams(dispatch, 'softenc', res.data.data.softencs, false)
    }
    dispatch(types.REFRESH_SOFTENCS_END)
  })
}

//superhdscreen
export function doLoadSuperHdScreens({dispatch, state}, {init = false} = {}) {
  if (init && state.superhdscreen.init) {
    return
  }
  dispatch(types.REFRESH_SUPERHDSCREENS_START)
  streamSrcApi.getItems('superhdscreens').then(function(res) {
    if (res && res.data && res.data.code === 0) {
      dispatch(types.SET_SUPERHDSCREENS, res.data.data.superhdscreens)
      addStreams(dispatch, 'superhdscreen', res.data.data.superhdscreens, false)
    }
    dispatch(types.REFRESH_SUPERHDSCREENS_END)
  })
}

//mtsrc
export function doLoadMtSrcs({dispatch, state}, {init = false} = {}) {
  if (init && state.mtsrc.init) {
    return
  }
  dispatch(types.REFRESH_MTSRCS_START)
  streamSrcApi.getItems('mtsrcs').then(function(res) {
    if (res && res.data && res.data.code === 0) {
      dispatch(types.SET_MTSRCS, res.data.data.mtsrcs)
      addStreams(dispatch, 'mtsrc', res.data.data.mtsrcs, false)
    }
    dispatch(types.REFRESH_MTSRCS_END)
  })
}

//ipcsrc
export function doLoadIpcSrcs({dispatch, state}, {init = false} = {}) {
  if (init && state.ipcsrc.init) {
    return
  }
  dispatch(types.REFRESH_IPCSRCS_START)
  streamSrcApi.getItems('ipcameras').then(function(res) {
    if (res && res.data && res.data.code === 0) {
      dispatch(types.SET_IPCSRCS, res.data.data.ipcameras)
      addStreams(dispatch, 'ipc', res.data.data.ipcameras, false)
    }
    dispatch(types.REFRESH_IPCSRCS_END)
  })
}

//encoder
export function doLoadEncoders({dispatch, state}, {init = false} = {}) {
  if (init && state.encoder.init) {
    return
  }
  dispatch(types.REFRESH_ENCODERS_START)
  streamSrcApi.getItems('encoders').then(function(res) {
    if (res && res.data && res.data.code === 0) {
      dispatch(types.SET_ENCODERS, res.data.data.encoders)
      addStreams(dispatch, 'encoder', res.data.data.encoders, true)
    }
    dispatch(types.REFRESH_ENCODERS_END)
  })
}

//proxysrc
export function doLoadProxySrcs({dispatch, state}, {init = false} = {}) {
  if (init && state.proxysrc.init) {
    return
  }
  dispatch(types.REFRESH_PROXYSRCS_START)
  streamSrcApi.getItems('proxys').then(function(res) {
    if (res && res.data && res.data.code === 0) {
      dispatch(types.SET_PROXYSRCS, res.data.data.proxys)
      addStreams(dispatch, 'proxy', res.data.data.proxys, false)
    }
    dispatch(types.REFRESH_PROXYSRCS_END)
  })
}





